﻿using System;
using System.Linq;
using Notoric.Commanding;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command for setting the stem direction of chords in a selection.
    /// </summary>
    public class SetStemDirectionCommand : MusicSelectionRibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SetStemDirectionCommand"/> class.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// The main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public SetStemDirectionCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Change Stem Direction";
            ScreenTipText = "Changes the stem orientation of the selected notes to the indicated direction.";
            DisableReason = "Select one or more notes first.";
        }

        #endregion

        #region MusicSelectionRibbonCommand Members

        /// <inheritdoc/>
        protected override bool CanExecuteCore(object parameter)
        {
            return Selection != null && Selection.Any(s => s is ViewModel.Elements.CWNotation.Chord);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            if (parameter != null)
            {
                var directon = parameter.ToString().ToLower();
                ViewModel.Elements.CWNotation.StemOrientation stemDirection;
                if (directon == "up")
                    stemDirection = ViewModel.Elements.CWNotation.StemOrientation.Up;
                else if (directon == "down")
                    stemDirection = ViewModel.Elements.CWNotation.StemOrientation.Down;
                else
                    return;

                var macro = new UndoableMacroCommand(ApplicationWindow.StackManager.ActiveUndoStack, ApplicationWindow.StackManager.ActiveRedoStack)
                {
                    Name = "Set Stem Direction"
                };

                var chords = from symbol in Selection
                             where symbol is ViewModel.Elements.CWNotation.Chord
                             select (ViewModel.Elements.CWNotation.Chord)symbol;

                ViewModel.Elements.CWNotation.MusicSectionInterior interior = null;
                if (chords.Count() == 0)
                    return;
                else
                    interior = chords.First().FindOwner<ViewModel.Elements.CWNotation.MusicSectionInterior>();

                foreach (var chord in chords)
                    macro.Commands.Add(chord.CreateSetStemDirectionCommand(stemDirection));

                macro.Commands.Add(
                    new ViewModel.MusicSectionInteriorLayoutCommand(ApplicationWindow.StackManager.ActiveUndoStack, ApplicationWindow.StackManager.ActiveRedoStack)
                    {
                        Interior = interior
                    }
                    );
                macro.Execute(null);
            }
        }

        #endregion
    }
}
